const { Controller } = require('egg');

class OrderController extends Controller {

  /**
   * @api {GET} /order/order/list 获取订单列表
   * @apiName list
   * @apiGroup order
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } name 订单名称
   * @apiQuery  { String } describe 备注
   * @apiQuery  { Number } status 类型
   * @apiQuery  { String } order 排序字段
   * @apiQuery  { String } method 排序方式
   * @apiQuery  { String } pageIndex 页码
   * @apiQuery  { String } pageSize 页面大小
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async list() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const param = ctx.query;

      const { name, describe, status, order, method, pageIndex, pageSize } = await app.rule.order.list(param);

      const data = await ctx.service.order.list(name, describe, status, order, method, pageIndex, pageSize);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {POST} /order/order/insert 新增
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } name 产品id
   * @apiBody  { String } describe 描述
   * @apiBody  { String } address_id 地址id
   * @apiBody  { String } type 类型
   * @apiBody  { String } level 阶段
   * @apiBody  { String } status 状态
   * @apiBody  { String } product 产品
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async insert() {
    const ctx = this.ctx;
    const app = this.app;
    try {
      const param = ctx.request.body;
      const { name, describe, address_id, type, level, status, product } = await app.rule.order.insert(param);

      const data = await ctx.service.order.insert(name, describe, address_id, type, level, status, product);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {POST} /order/order/cancel 取消订单
   * @apiName list
   * @apiGroup order
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } order_id 订单名称
   * @apiQuery  { String } remark 备注
   * @apiQuery  { String } order 排序字段
   * @apiQuery  { String } method 排序方式
   * @apiQuery  { String } pageIndex 页码
   * @apiQuery  { String } pageSize 页面大小
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async cancel() {
    const ctx = this.ctx;
    const app = this.app;

    try {

      const param = ctx.query;

      const { order_id, remark } = await app.rule.order.cancel(param);

      const data = await ctx.service.order.cancel(order_id, remark);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api {POST} /order/order/finish 完成订单
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } order_id 订单id
   * @apiBody  { String } remark 备注
   * @apiBody  { String } barcode 条码
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async finish() {
    const ctx = this.ctx;
    const app = this.app;
    try {
      const param = ctx.request.body;

      const { order_id, remark } = await app.rule.order.finish(param);

      const data = await ctx.service.order.finish(order_id, remark);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {GET} /order/order/change 改变地址
   * @apiName list
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } address_id id
   * @apiQuery  { String } order_id 订单id
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async change() {
    const ctx = this.ctx;
    const app = this.app;

    try {

      const param = ctx.query;

      const { address_id, order_id } = await app.rule.order.change(param);

      const data = await ctx.service.order.change(address_id, order_id);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api {POST} /order/order/refuse 拒绝订单
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } order_id 订单id
   * @apiBody  { String } remark 备注
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async refuse() {
    const ctx = this.ctx;
    const app = this.app;

    try {

      const param = ctx.request.body;

      const { order_id, remark } = await app.rule.order.refuse(param);

      const data = await ctx.service.order.refuse(order_id, remark);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api {GET} /order/order/delete 拒绝订单
   * @apiName delete
   * @apiGroup order
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } order_id 订单id
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async delete() {
    const { ctx, app } = this;

    try {
      const param = ctx.request.body;

      const { ids } = await app.rule.order.delete(param);

      const data = await ctx.service.order.delete(ids);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;

    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = OrderController;
